<template>
  <div class="health-analysis">
    <!-- 健康评分卡片 -->
    <el-card class="mb-6">
      <template #header>
        <div class="flex justify-between items-center">
          <span class="text-lg font-semibold">健康评分</span>
          <el-button type="primary" size="small" @click="recalculateScore">
            重新计算
          </el-button>
        </div>
      </template>
      
      <div class="text-center">
        <el-progress
          type="circle"
          :percentage="healthScore.score"
          :color="getScoreColor(healthScore.score)"
          :width="120"
          class="mb-4"
        />
        <div class="text-2xl font-bold mb-2" :class="getScoreColor(healthScore.score)">
          {{ healthScore.score }}分
        </div>
        <div class="text-lg text-gray-600 mb-4">
          健康等级：{{ healthScore.grade }}
        </div>
        <div v-if="healthScore.issues.length > 0" class="text-sm text-red-600">
          <div class="mb-2">需要关注的问题：</div>
          <ul class="list-disc list-inside">
            <li v-for="issue in healthScore.issues" :key="issue">{{ issue }}</li>
          </ul>
        </div>
      </div>
    </el-card>

    <!-- 健康建议 -->
    <el-card>
      <template #header>
        <span class="text-lg font-semibold">健康建议</span>
      </template>
      
      <div v-if="healthScore.recommendations.length > 0" class="space-y-4">
        <div
          v-for="(recommendation, index) in healthScore.recommendations"
          :key="index"
          class="p-4 rounded-lg border"
          :class="getRecommendationClass(recommendation.type)"
        >
          <div class="flex items-start">
            <el-icon class="mt-1 mr-3" :class="getIconColor(recommendation.type)">
              <Warning v-if="recommendation.type === 'warning'" />
              <InfoFilled v-if="recommendation.type === 'info'" />
            </el-icon>
            <div class="flex-1">
              <div class="font-semibold mb-1">{{ recommendation.title }}</div>
              <div class="text-sm text-gray-600">{{ recommendation.content }}</div>
              <div v-if="recommendation.priority" class="text-xs mt-2">
                <el-tag :type="getPriorityType(recommendation.priority)" size="small">
                  {{ getPriorityText(recommendation.priority) }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>
      </div>
      
      <div v-else class="text-center text-gray-500 py-8">
        暂无健康建议
      </div>
    </el-card>

    <!-- 详细分析 -->
    <el-card class="mt-6" v-if="showDetails">
      <template #header>
        <span class="text-lg font-semibold">详细分析</span>
      </template>
      
      <div class="space-y-6">
        <!-- BMI分析 -->
        <div v-if="healthData.bmi" class="analysis-item">
          <div class="text-lg font-semibold mb-2">BMI指数分析</div>
          <div class="grid grid-cols-2 gap-4">
            <div>
              <span class="text-gray-600">当前BMI：</span>
              <span class="font-semibold">{{ bmiAnalysis.value }}</span>
            </div>
            <div>
              <span class="text-gray-600">状态：</span>
              <span class="font-semibold" :class="bmiAnalysis.statusColor">
                {{ bmiAnalysis.status }}
              </span>
            </div>
          </div>
        </div>

        <!-- 血压分析 -->
        <div v-if="healthData.bloodPressure" class="analysis-item">
          <div class="text-lg font-semibold mb-2">血压分析</div>
          <div class="grid grid-cols-2 gap-4">
            <div>
              <span class="text-gray-600">收缩压：</span>
              <span class="font-semibold">{{ healthData.bloodPressure.systolic }} mmHg</span>
            </div>
            <div>
              <span class="text-gray-600">舒张压：</span>
              <span class="font-semibold">{{ healthData.bloodPressure.diastolic }} mmHg</span>
            </div>
            <div class="col-span-2">
              <span class="text-gray-600">状态：</span>
              <span class="font-semibold" :class="bloodPressureAnalysis.statusColor">
                {{ bloodPressureAnalysis.status }}
              </span>
            </div>
          </div>
          <div class="mt-2 text-sm text-gray-600">
            {{ bloodPressureAnalysis.advice }}
          </div>
        </div>

        <!-- 心率分析 -->
        <div v-if="healthData.heartRate && healthData.age" class="analysis-item">
          <div class="text-lg font-semibold mb-2">心率分析</div>
          <div class="grid grid-cols-2 gap-4">
            <div>
              <span class="text-gray-600">当前心率：</span>
              <span class="font-semibold">{{ healthData.heartRate }} bpm</span>
            </div>
            <div>
              <span class="text-gray-600">最大心率：</span>
              <span class="font-semibold">{{ heartRateAnalysis.maxHeartRate }} bpm</span>
            </div>
            <div class="col-span-2">
              <span class="text-gray-600">状态：</span>
              <span class="font-semibold" :class="heartRateAnalysis.statusColor">
                {{ heartRateAnalysis.status }}
              </span>
            </div>
          </div>
          <div class="mt-2 text-sm text-gray-600">
            {{ heartRateAnalysis.advice }}
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { ElCard, ElProgress, ElButton, ElIcon, ElTag } from 'element-plus'
import { Warning, InfoFilled } from '@element-plus/icons-vue'
import { calculateHealthScore, calculateBMI, analyzeBloodPressure, analyzeHeartRate } from '../utils/healthAnalysis'

const props = defineProps({
  healthData: {
    type: Object,
    default: () => ({
      bmi: { weight: 70, height: 175 },
      bloodPressure: { systolic: 120, diastolic: 80 },
      heartRate: 75,
      age: 30
    })
  },
  showDetails: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['score-calculated'])

const healthScore = reactive({
  score: 0,
  grade: '',
  issues: [],
  recommendations: []
})

const bmiAnalysis = computed(() => {
  if (!props.healthData.bmi) return null
  return calculateBMI(props.healthData.bmi.weight, props.healthData.bmi.height)
})

const bloodPressureAnalysis = computed(() => {
  if (!props.healthData.bloodPressure) return null
  return analyzeBloodPressure(props.healthData.bloodPressure.systolic, props.healthData.bloodPressure.diastolic)
})

const heartRateAnalysis = computed(() => {
  if (!props.healthData.heartRate || !props.healthData.age) return null
  return analyzeHeartRate(props.healthData.heartRate, props.healthData.age)
})

const recalculateScore = () => {
  const scoreData = calculateHealthScore(props.healthData)
  Object.assign(healthScore, scoreData)
  emit('score-calculated', scoreData)
}

const getScoreColor = (score) => {
  if (score >= 90) return 'text-green-500'
  if (score >= 80) return 'text-blue-500'
  if (score >= 70) return 'text-yellow-500'
  if (score >= 60) return 'text-orange-500'
  return 'text-red-500'
}

const getRecommendationClass = (type) => {
  const classes = {
    warning: 'bg-red-50 border-red-200',
    info: 'bg-blue-50 border-blue-200'
  }
  return classes[type] || 'bg-gray-50 border-gray-200'
}

const getIconColor = (type) => {
  const colors = {
    warning: 'text-red-500',
    info: 'text-blue-500'
  }
  return colors[type] || 'text-gray-500'
}

const getPriorityType = (priority) => {
  const types = {
    high: 'danger',
    medium: 'warning',
    low: 'info'
  }
  return types[priority] || 'info'
}

const getPriorityText = (priority) => {
  const texts = {
    high: '高优先级',
    medium: '中优先级',
    low: '低优先级'
  }
  return texts[priority] || '普通'
}

// 初始化计算
watch(() => props.healthData, recalculateScore, { immediate: true })
</script>

<style scoped>
.health-analysis {
  max-width: 800px;
  margin: 0 auto;
}

.analysis-item {
  padding: 16px;
  background-color: #f8fafc;
  border-radius: 8px;
  border-left: 4px solid #3b82f6;
}

.analysis-item:not(:last-child) {
  margin-bottom: 16px;
}
</style>